# -*- coding: utf-8 -*-
#
# Author:: Jonny
# Date:: 2022/7/16
import os


def tx_code_to_ts_code(tx_code):
    ts_code = tx_code[2:] + "." + tx_code[:2].upper()
    return ts_code


def tx_code_to_ths_code(tx_code):
    ths_code = tx_code[:2] + "_" + tx_code[2:]
    return ths_code


def ts_code_to_tx_code(ts_code):
    list_tmp = ts_code.split(".")
    tx_code = list_tmp[1].replace(".", "").lower() + list_tmp[0]
    return tx_code


def ts_code_to_ths_code(ts_code):
    list_tmp = ts_code.split(".")
    tx_code = list_tmp[1].replace(".", "").lower() + "_" + list_tmp[0]
    return tx_code


def trans_ths_txt_to_list(txt_path):
    """

    return:
    ['SH513220', '全球互联ETF', '+0.73%', '0.971', '+0.007', '5.62%', '6.12%', '+0.00%', '7145092', '127307227', '127307227', '7364600', '无', '无', '--', '0', '0', '0', '20400↓', '0.955', '0.964', '0.986', '0.927', '0.971', '0.972', '238000', '1900', '+49.41', '--', '0', '29109', '4355500', '3009100']
    """
    f = open(txt_path, "r", encoding="GBK")
    count = 0
    data_list = []
    for line in f.readlines():
        count += 1
        if count == 1:
            continue
        if len(line.strip("\n")) < 1:
            continue
        for i in range(10):
            if "\t\t" in line:
                line = line.replace("\t\t", "\t")
            else:
                break
        tmp_list = line.strip("\n").strip("\t").split("\t")
        data_list.append(tmp_list)
    return data_list


if __name__ == "__main__":

    def test_tx_code_to_ths_code():
        tx_code = "sh000001"
        ths_code = tx_code_to_ths_code(tx_code)
        print(ths_code)

    def test_ts_code_to_ths_code():
        tx_code = "000001.SH"
        ths_code = ts_code_to_ths_code(tx_code)
        print(ths_code)

    def test_trans_ths_txt_to_list():
        from submodules.common.lib import local_paths

        project_path = local_paths.project_path
        txt_path = os.path.join(project_path, "data", "ETF", "Table.txt")
        l1 = trans_ths_txt_to_list(txt_path)
        print(l1)

    test_trans_ths_txt_to_list()
